package test;

import java.util.Properties;
import java.util.UUID;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerMetaData;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import redis.clients.jedis.ShardedJedis;
import test.redis.JedisUtil;

public class JobDemo implements Job{
	public void  execute(JobExecutionContext context) throws JobExecutionException {
			String currentClusterId = "";
			try {
				currentClusterId = context.getScheduler().getSchedulerName();
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
			ShardedJedis resource = JedisUtil.pool.getResource();
			String clusterId = resource.get("quartz_scheduler_clusterId");
			resource.close();
			System.out.println(currentClusterId);
			System.out.println(clusterId);
		if (currentClusterId.equals(clusterId)) {
			long id = Thread.currentThread().getId();
			String name = context.getJobDetail().getKey().getName();
			System.out.println("抢了一个咬一口=============:" + name+"：》线程："+id);
		} else {
			long id = Thread.currentThread().getId();
			System.out.println("我没有资格:"+id);
		}
	}

	public static void main(String[] args) throws Exception {
		new Thread(){public void run() {
			try {
				task();
			} catch (Exception e) {
				e.printStackTrace();
			}
		};}.start();
		new Thread(){public void run() {
			try {
				task();
			} catch (Exception e) {
				e.printStackTrace();
			}
		};}.start();
		new Thread(){public void run() {
			try {
				task();
			} catch (Exception e) {
				e.printStackTrace();
			}
		};}.start();
		new Thread(){public void run() {
			try {
				task();
			} catch (Exception e) {
				e.printStackTrace();
			}
		};}.start();
		new Thread(){public void run() {
			try {
				task();
			} catch (Exception e) {
				e.printStackTrace();
			}
		};}.start();
	}

	private static void task() throws SchedulerException, InterruptedException {
			
		while(true){
			ShardedJedis resource = JedisUtil.pool.getResource();
			String quartz_scheduler_clusterId = resource.get("quartz_scheduler_clusterId");
			resource.close();
			if (quartz_scheduler_clusterId==null) {
				qidong();
			}else{
				resource.setex("quartz_scheduler_clusterId", 6,quartz_scheduler_clusterId);
			}
			System.out.println("getNumActive:"+JedisUtil.pool.getNumActive());
			System.out.println("getNumIdle:"+JedisUtil.pool.getNumIdle());
			Thread.sleep(10000);
			
		}
		
	}

	private static void qidong() throws SchedulerException, InterruptedException {
		ShardedJedis resource = JedisUtil.pool.getResource();
		String clusterId = "scheduler-"+UUID.randomUUID().toString();
		Long setnx = resource.setnx("quartz_scheduler_clusterId", clusterId);
		Scheduler scheduler =null;
		if (setnx==1) {
			
			StdSchedulerFactory sf = new StdSchedulerFactory();
			Properties props = new Properties();
			props.put("org.quartz.scheduler.instanceName",clusterId);
			props.put("org.quartz.threadPool.threadCount", "10");
			sf.initialize(props);
			scheduler = sf.getScheduler();
			System.out.println("getSchedulerInstanceId:>>"+scheduler.getSchedulerName());
			JobDetail job = JobBuilder.newJob(JobDemo.class).withIdentity("method1", "class1").build();
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("method1", "class1")
					.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();
			scheduler.scheduleJob(job, trigger);
			scheduler.start();
			Thread.sleep(5000);
			scheduler.shutdown(true);
			resource = JedisUtil.pool.getResource();
			resource.del("quartz_scheduler_clusterId");
			SchedulerMetaData metaData = scheduler.getMetaData();
			System.out.println(metaData.getSchedulerName()+"：tasked >>:"+metaData.getNumberOfJobsExecuted());
		}
		resource.close();
	}


}